<template>
  <div class="dashboard">
    <!-- 统计卡片 -->
    <el-row :gutter="20" style="margin-bottom: 20px">
      <el-col :span="6">
        <el-card class="stat-card stat-blue" shadow="hover">
          <div class="stat-label">用户数</div>
          <div class="stat-value">{{ stats.userCount }}</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="stat-card stat-green" shadow="hover">
          <div class="stat-label">题目数</div>
          <div class="stat-value">{{ stats.questionCount }}</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="stat-card stat-orange" shadow="hover">
          <div class="stat-label">考试次数</div>
          <div class="stat-value">{{ stats.examCount }}</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="stat-card stat-red" shadow="hover">
          <div class="stat-label">通过率</div>
          <div class="stat-value">{{ stats.passRate }}</div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 最近统计 -->
    <el-card shadow="never">
      <template #header>
        <div class="card-header">
          <span>📊 最近统计</span>
        </div>
      </template>

      <el-row :gutter="20">
        <el-col :span="12">
          <div class="stats-item">
            <span class="stats-label">今日新增用户</span>
            <span class="stats-number">{{ stats.todayUsers }}</span>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="stats-item">
            <span class="stats-label">本周考试次数</span>
            <span class="stats-number">{{ stats.weekExams }}</span>
          </div>
        </el-col>
      </el-row>

      <el-row :gutter="20" style="margin-top: 15px">
        <el-col :span="12">
          <div class="stats-item">
            <span class="stats-label">题库提交分类</span>
            <span class="stats-number">64</span>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="stats-item">
            <span class="stats-label">平均分(近20次)</span>
            <span class="stats-number">{{ stats.avgScore }}</span>
          </div>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, onMounted } from 'vue'
import { getLocalData } from '@/utils/storage'

const stats = reactive({
  userCount: 2,
  questionCount: 455,
  examCount: 1,
  passRate: '0%',
  todayUsers: 0,
  weekExams: 0,
  avgScore: 0
})

const loadStats = () => {
  const users = getLocalData('users')
  const questions = getLocalData('questions')
  const records = getLocalData('records')

  stats.userCount = users.length
  stats.questionCount = questions.length
  stats.examCount = records.length

  const passedCount = records.filter((r) => r.is_passed === 1).length
  stats.passRate =
    records.length > 0 ? Math.round((passedCount / records.length) * 100) + '%' : '0%'
}

onMounted(() => {
  loadStats()
})
</script>

<style scoped>
.dashboard {
  width: 100%;
}

.stat-card {
  color: white;
  border: none;
  transition: all 0.2s ease;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12) !important;
}

.stat-card.stat-blue {
  background: #1890ff;
  box-shadow: 0 2px 8px rgba(24, 144, 255, 0.2);
}

.stat-card.stat-green {
  background: #52c41a;
  box-shadow: 0 2px 8px rgba(82, 196, 26, 0.2);
}

.stat-card.stat-orange {
  background: #fa8c16;
  box-shadow: 0 2px 8px rgba(250, 140, 22, 0.2);
}

.stat-card.stat-red {
  background: #f5222d;
  box-shadow: 0 2px 8px rgba(245, 34, 45, 0.2);
}

.stat-label {
  font-size: 14px;
  opacity: 0.9;
  margin-bottom: 10px;
  font-weight: 400;
}

.stat-value {
  font-size: 36px;
  font-weight: 600;
}

.stats-item {
  padding: 20px;
  background: white;
  border-radius: 6px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border: 1px solid #e8e8e8;
  transition: all 0.2s ease;
}

.stats-item:hover {
  border-color: #d9d9d9;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.stats-label {
  color: #666;
  font-size: 14px;
  font-weight: 400;
}

.stats-number {
  font-size: 24px;
  font-weight: 600;
  color: #333;
}

.card-header {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}
</style>

